package com.sx.pipeline.ui.wallet.recharge

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.sx.mvvmlibs.base.BaseViewModel
import com.sx.mvvmlibs.event.SingleLiveEvent
import com.sx.mvvmlibs.http.CommonObserver
import com.sx.mvvmlibs.utils.RxThreadHelper
import com.sx.pipeline.app.model.CYBaseViewModel
import com.sx.pipeline.repos.http.res.*

/**
 *    author : zhouyouqiang
 *    e-mail : 1638473898@qq.com
 *    date   : 2022/5/20 13:51
 *    desc   :
 *    version: 1.0
 */
class RechargeVM(app: Application) : CYBaseViewModel(app) {
    /**
     * 充值套餐列表
     */
    val rechargeListEvent: SingleLiveEvent<List<RechargeInfoVO>> = SingleLiveEvent()
    fun getRechargeList() {
        addSubscribe(
            mModel.getRechargeList()
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<List<RechargeInfoVO>, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: List<RechargeInfoVO>) {
                        rechargeListEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        dismissLoadingDialog()
                    }
                })
        )
    }


    /**
     * 微信充值
     */
    val rechargeDataEvent: SingleLiveEvent<WechatResponse> = SingleLiveEvent()
    fun sumitWxRecharge(id:Int) {
        addSubscribe(
            mModel.sumitWxRecharge(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<WechatResponse, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: WechatResponse) {
                        rechargeDataEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        dismissLoadingDialog()
                    }
                })
        )
    }

    val rechargeAliPayDataEvent: MutableLiveData<String> = MutableLiveData()
    fun sumitAliPayRecharge(id:Int) {
        addSubscribe(
            mModel.submitAliPayRecharge(id)
                .compose(RxThreadHelper.rxSchedulerHelper())
                .subscribeWith(object : CommonObserver<String, BaseViewModel<*>>
                    (this, false) {
                    override fun onResult(result: String) {
                        rechargeAliPayDataEvent.postValue(result)
                        dismissLoadingDialog()
                    }

                    override fun onFailed(code: Int, msg: String?) {
                        super.onFailed(code, msg)
                        ToastUtils.showShort(msg)
                        dismissLoadingDialog()
                    }
                })
        )
    }
}